/*
 * function TreeNode(x) {
 *   this.val = x;
 *   this.left = null;
 *   this.right = null;
 * }
 */

/**
  * 
  * @param root TreeNode类 
  * @param sum int整型 
  * @return bool布尔型
  */
 function hasPathSum( root ,  sum ) {
  // write code here
  if(root === null) return false
  let path = []
  function findPath(pRoot,target){
    if(pRoot === null) return
    path.push(pRoot.val)
    target -= pRoot.val
    if(target === 0 && pRoot.left === null && pRoot.right === null){
      return true
    }
    let res = findPath(pRoot.left,target) || findPath(pRoot.right,target)
    path.pop()
    return res
  }
  return findPath(root,sum)
}
module.exports = {
  hasPathSum : hasPathSum
};

/**
  * 
  * @param root TreeNode类 
  * @param sum int整型 
  * @return bool布尔型
  */
 let path = []
 function hasPathSum( root ,  sum ) {
  // write code here
  if(root === null) return false
  path.push(root.val)
  sum -= root.val
  if(sum === 0 && root.left === null && root.right === null){
    return true
  }
  let flag = hasPathSum(root.left,sum) || hasPathSum(root.right,sum)
  path.pop()
  return flag
}
module.exports = {
  hasPathSum : hasPathSum
};